package.path = "./plugin/MingnuoAir/utils.lua"..";..\\?.lua"
package.path = "./plugin/MingnuoAir/json.lua"..";..\\?.lua"

require "utils.lua"
require "json.lua"

rxBuf = {}
regAddr = 0x0000

function NewVariables()
    -- body
    local VariableMap = {
        Variable = {
        },
    }

    return VariableMap
end

function GenerateGetRealVariables(sAddr,step)

    -- body
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(step == 0)
    then
        -----------读运行状态----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x01
        requestADU[3] = 0x00
        requestADU[4] = 0x22
        requestADU[5] = 0x00
        requestADU[6] = 0x18
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0x0022
        VariableMap = {
            Status = "1",
            Variable = requestADU
        }
    elseif(step == 1)
    then
        ----------读运行参数-----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x03
        requestADU[3] = 0x00
        requestADU[4] = 0x00
        requestADU[5] = 0x00
        requestADU[6] = 0x19
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0x0000
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    end


    return VariableMap
end

function DeviceCustomCmd(sAddr,cmdName,cmdParam,step)

    -- body
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }
    local temp = 0

    if(cmdName == "GetRealVariables")
    then
        if(step == 0)
        then
            -----------读运行状态----------------
            requestADU[1] = sAddr
            requestADU[2] = 0x01
            requestADU[3] = 0x00
            requestADU[4] = 0x22
            requestADU[5] = 0x00
            requestADU[6] = 0x18
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)

            regAddr = 0x0022
            VariableMap = {
                Status = "1",
                Variable = requestADU
            }
        elseif(step == 1)
        then
            ----------读运行参数-----------------
            requestADU[1] = sAddr
            requestADU[2] = 0x03
            requestADU[3] = 0x00
            requestADU[4] = 0x00
            requestADU[5] = 0x00
            requestADU[6] = 0x19
            local VariableMapTemp = {
                Variable = requestADU
            }
            crc = GetCRCModbus(VariableMapTemp)
            requestADU[7] = math.modf(crc%256)
            requestADU[8] = math.modf(crc/256)

            regAddr = 0x0000
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        end
    elseif(cmdName == "SetOpen")
    then
        -----------读运行状态----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x05
        requestADU[3] = 0x00
        requestADU[4] = 0x29
        requestADU[5] = 0xFF
        requestADU[6] = 0x00
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0x0029
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    elseif(cmdName == "SetClose")
    then
        -----------读运行状态----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x05
        requestADU[3] = 0x00
        requestADU[4] = 0x29
        requestADU[5] = 0x00
        requestADU[6] = 0x00
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0x0029
        VariableMap = {
            Status = "0",
            Variable = requestADU
        } 
    elseif(cmdName == "SetCoolOpeningTemp")
    then
        -----------读运行状态----------------
        res = json.jsondecode(cmdParam)
        temp = tonumber(res["Temp"])*10

        requestADU[1] = sAddr
        requestADU[2] = 0x06
        requestADU[3] = 0x00
        requestADU[4] = 0x0D
        requestADU[5] = math.modf(temp/256)
        requestADU[6] = math.modf(temp%256)
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0x000D
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }   
    elseif(cmdName == "SetCoolClosingTemp")
    then
        -----------读运行状态----------------
        res = json.jsondecode(cmdParam)
        temp = tonumber(res["Temp"])*10

        requestADU[1] = sAddr
        requestADU[2] = 0x06
        requestADU[3] = 0x00
        requestADU[4] = 0x0E
        requestADU[5] = math.modf(temp/256)
        requestADU[6] = math.modf(temp%256)
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0x000E
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }   
    elseif(cmdName == "SetHotOpeningTemp")
    then
        -----------读运行状态----------------
        res = json.jsondecode(cmdParam)
        temp = tonumber(res["Temp"])*10

        requestADU[1] = sAddr
        requestADU[2] = 0x06
        requestADU[3] = 0x00
        requestADU[4] = 0x0F
        requestADU[5] = math.modf(temp/256)
        requestADU[6] = math.modf(temp%256)
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0x000F
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }  
    elseif(cmdName == "SetHotClosingTemp")
    then
        -----------读运行状态----------------
        res = json.jsondecode(cmdParam)
        temp = tonumber(res["Temp"])*10

        requestADU[1] = sAddr
        requestADU[2] = 0x06
        requestADU[3] = 0x00
        requestADU[4] = 0x10
        requestADU[5] = math.modf(temp/256)
        requestADU[6] = math.modf(temp%256)
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0x0010
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }                      
    end

    return VariableMap
end

function AnalysisRx(sAddr,rxBufCnt)

    local index = 1
    local mAddr = 0
    local mFunCode = 0
    local mRegByte = 0
    local crcH,crcL,crc,crcTemp

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }

    --modbus03 06 最短为8个字节
    if(rxBufCnt < 8)
    then
        rxBuf = {}
        return VariableMap
    end


    for index=1,rxBufCnt,1 do
        if(index <= rxBufCnt)
        then
            mAddr     = rxBuf[index]
            mFunCode  = rxBuf[index+1]
            if (mAddr == tonumber(sAddr))
            then
                if(mFunCode == 0x03)
                then
                    mRegByte = rxBuf[index+2]
                    if(mRegByte+5 > rxBufCnt)
                    then
                        rxBuf = {}
                        return VariableMap
                    end
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+2+mRegByte,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+3+mRegByte+1]*256+rxBuf[index+3+mRegByte]
                    if(crc == crcTemp)
                    then
                        local Variable = {}
                        local varIndex = 3
                        local VariableIndex = 0
                        if(regAddr == 0x0000)
                        then
                            ---------------------------柜内温度----------------------------------
                            local InsideTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"InsideTemp","","",InsideTemp,InsideTemp)
                            VariableIndex = VariableIndex + 1
                            ----------------------------蒸发器温度---------------------------------
                            local EvaporatorTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"EvaporatorTemp","","",EvaporatorTemp,EvaporatorTemp)
                            VariableIndex = VariableIndex + 1
                            -----------------------------冷凝器温度--------------------------------
                            local CondenserTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CondenserTemp","","",CondenserTemp,CondenserTemp)
                            VariableIndex = VariableIndex + 1
                            ------------------------------柜外温度-------------------------------
                            local OutSideTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutSideTemp","","",OutSideTemp,OutSideTemp)
                            VariableIndex = VariableIndex + 1
                            -----------------------------交流电压--------------------------------
                            local AlternatingVoltage = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"AlternatingVoltage","","",AlternatingVoltage,AlternatingVoltage)
                            VariableIndex = VariableIndex + 1
                            -----------------------------直流电压--------------------------------
                            local DirectVoltage = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"DirectVoltage","","",DirectVoltage,DirectVoltage)
                            VariableIndex = VariableIndex + 1
                            -----------------------------内风机转速--------------------------------
                            local IndoorFanSpeed = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"IndoorFanSpeed","","",IndoorFanSpeed,IndoorFanSpeed)
                            VariableIndex = VariableIndex + 1
                            ----------------------------外风机转速---------------------------------
                            local OutdoorFanSpeed = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutdoorFanSpeed","","",OutdoorFanSpeed,OutdoorFanSpeed)
                            VariableIndex = VariableIndex + 1
                            -----------------------------湿度值-------------------------------
                            local Humi = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"Humi","","",Humi,Humi)
                            VariableIndex = VariableIndex + 1
                            -----------------------------保留--------------------------------
                            varIndex = varIndex + 2
                            -----------------------------保留--------------------------------
                            varIndex = varIndex + 2
                            -----------------------------保留--------------------------------
                            varIndex = varIndex + 2
                            -----------------------------保留--------------------------------
                            varIndex = varIndex + 2
                            ------------------------------制冷开启温度-------------------------------
                            local CoolOpeningTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CoolOpeningTemp","","",CoolOpeningTemp,CoolOpeningTemp)
                            VariableIndex = VariableIndex + 1
                            ------------------------------制冷停止温度-------------------------------
                            local CoolClosingTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CoolClosingTemp","","",CoolClosingTemp,CoolClosingTemp)
                            VariableIndex = VariableIndex + 1
                            -------------------------------加热开启温度------------------------------
                            local HotOpeningTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HotOpeningTemp","","",HotOpeningTemp,HotOpeningTemp)
                            VariableIndex = VariableIndex + 1
                            -----------------------------加热停止温度--------------------------------
                            local HotClosingTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HotClosingTemp","","",HotClosingTemp,HotClosingTemp)
                            VariableIndex = VariableIndex + 1
                            ------------------------------柜内温度高温告警点-------------------------------
                            local InsideHighTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"InsideHighTemp","","",InsideHighTemp,InsideHighTemp)
                            VariableIndex = VariableIndex + 1
                            -------------------------------柜内温度低温告警点------------------------------
                            local InsideLowTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"InsideLowTemp","","",InsideLowTemp,InsideLowTemp)
                            VariableIndex = VariableIndex + 1
                            --------------------------------冷凝器温度高温度保护点-----------------------------
                            local CondenserHighTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CondenserHighTemp","","",CondenserHighTemp,CondenserHighTemp)
                            VariableIndex = VariableIndex + 1
                            ---------------------------------蒸发器温度冻结保护点----------------------------
                            local EvaporatorFreezeTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"EvaporatorFreezeTemp","","",EvaporatorFreezeTemp,EvaporatorFreezeTemp)
                            VariableIndex = VariableIndex + 1
                            ----------------------------------除湿开启湿度---------------------------
                            local DehumiOpeningTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"DehumiOpeningTemp","","",DehumiOpeningTemp,DehumiOpeningTemp)
                            VariableIndex = VariableIndex + 1
                            -----------------------------------除湿停止湿度--------------------------
                            local DehumiClosingTemp = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"DehumiClosingTemp","","",DehumiClosingTemp,DehumiClosingTemp)
                            VariableIndex = VariableIndex + 1
                            ----------------------------------湿度告警限值---------------------------
                            local HumiAlarm = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HumiAlarm","","",HumiAlarm,HumiAlarm)
                            VariableIndex = VariableIndex + 1
                            ----------------------------------湿度校正值---------------------------
                            local HumiCalib = rxBuf[index+varIndex]*256+rxBuf[index+varIndex+1]
                            varIndex = varIndex + 2
                            Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HumiCalib","","",HumiCalib,HumiCalib)
                            VariableIndex = VariableIndex + 1
                            -------------------------------------------------------------
                            VariableMap = {
                                Status = "0",
                                Variable = Variable
                            }
                        end
                        rxBuf = {}
                        return VariableMap
                    end
                elseif(mFunCode == 0x01)
                    then
                        mRegByte = rxBuf[index+2]
                        if(mRegByte+5 > rxBufCnt)
                        then
                            rxBuf = {}
                            return VariableMap
                        end
                        --取出数据域数据
                        local pdu = {}
                        for i=index,index+2+mRegByte,1 do
                            if rxBuf[i] ~= nil then
                                table.insert(pdu, rxBuf[i])
                            end
                        end
                        local VariableMapCRC = {
                            Variable = pdu
                        }
                        crc = GetCRCModbus(VariableMapCRC)
                        crcTemp = rxBuf[index+3+mRegByte+1]*256+rxBuf[index+3+mRegByte]
                        if(crc == crcTemp)
                        then
                            local Variable = {}
                            local varIndex = 3
                            local VariableIndex = 0
                            if(regAddr == 0x0022)
                            then
                                -------------------------------------------------------------
                                local status1 = rxBuf[index+varIndex]
                                local status2 = rxBuf[index+varIndex+1]
                                local status3 = rxBuf[index+varIndex+2]
                                varIndex = varIndex + 3
                                ----------------------制冷状态---------------------------------------
                                local CoolStatus = utils.And(status1,0x01)
                                if(CoolStatus == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CoolStatus","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CoolStatus","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ----------------------加热器状态---------------------------------------
                                local HotStatus = utils.And(status1,0x02)
                                if(HotStatus == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HotStatus","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HotStatus","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ----------------------内风机状态---------------------------------------
                                local IndoorFanStatus = utils.And(status1,0x04)
                                if(IndoorFanStatus == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"IndoorFanStatus","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"IndoorFanStatus","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                -----------------------外风机状态--------------------------------------
                                local OutdoorFanStatus = utils.And(status1,0x08)
                                if(OutdoorFanStatus == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutdoorFanStatus","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutdoorFanStatus","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                -----------------------应急风机状态--------------------------------------
                                local EmergencyFanStatus = utils.And(status1,0x10)
                                if(EmergencyFanStatus == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"EmergencyFanStatus","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"EmergencyFanStatus","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ------------------------开关量1-------------------------------------
                                local IOOut1 = utils.And(status1,0x20)
                                -------------------------开关量2------------------------------------
                                local IOOut2 = utils.And(status1,0x40)
                                --------------------------开关机-----------------------------------
                                local OpenCloseCmd = utils.And(status1,0x80)
                                if(OpenCloseCmd == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OpenCloseCmd","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OpenCloseCmd","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                --------------------------柜内传感器故障-----------------------------------
                                local InsideSensorAlarm = utils.And(status2,0x01)
                                if(InsideSensorAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"InsideSensorAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"InsideSensorAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                --------------------------蒸发器温度传感器故障-----------------------------------
                                local EvaporatorTempAlarm = utils.And(status2,0x02)
                                if(EvaporatorTempAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"EvaporatorTempAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"EvaporatorTempAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ---------------------------冷凝器温度传感器故障----------------------------------
                                local CondenserTempAlarm = utils.And(status2,0x04)
                                if(CondenserTempAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CondenserTempAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CondenserTempAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ---------------------------柜外温度传感器故障----------------------------------
                                local OutsideSensorError = utils.And(status2,0x08)
                                if(OutsideSensorError == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutsideSensorError","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutsideSensorError","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ---------------------------湿度传感器故障----------------------------------
                                local HumiSensorError = utils.And(status2,0x10)
                                if(HumiSensorError == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HumiSensorError","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HumiSensorError","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ----------------------------湿度过高报警---------------------------------
                                local HumiHighAlarm = utils.And(status2,0x20)
                                if(HumiHighAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HumiHighAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HumiHighAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ----------------------------内风机告警---------------------------------
                                local IndoorFanAlarm = utils.And(status2,0x40)
                                if(IndoorFanAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"IndoorFanAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"IndoorFanAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ----------------------------外风机告警---------------------------------
                                local OutdoorFanAlarm = utils.And(status2,0x80)
                                if(OutdoorFanAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutdoorFanAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"OutdoorFanAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ----------------------------冷凝器高压告警---------------------------------
                                local CondenserHighTempAlarm = utils.And(status3,0x01)
                                if(CondenserHighTempAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CondenserHighTempAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CondenserHighTempAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ---------------------------蒸发器冻结告警----------------------------------
                                local EvaporatorFreezeTempAlarm = utils.And(status3,0x02)
                                if(EvaporatorFreezeTempAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"EvaporatorFreezeTempAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"EvaporatorFreezeTempAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ----------------------------制冷告警---------------------------------
                                local CoolAlarm = utils.And(status3,0x04)
                                if(CoolAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CoolAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"CoolAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ----------------------------高温告警---------------------------------
                                local HighTempAlarm = utils.And(status3,0x08)
                                if(HighTempAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HighTempAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HighTempAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ----------------------------低温告警---------------------------------
                                local LowTempAlarm = utils.And(status3,0x10)
                                if(LowTempAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"LowTempAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"LowTempAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ----------------------------加热器告警---------------------------------
                                local HeaterAlarm = utils.And(status3,0x20)
                                if(HeaterAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HeaterAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HeaterAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                -----------------------------过电压告警--------------------------------
                                local HighVoltageAlarm = utils.And(status3,0x40)
                                if(HighVoltageAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HighVoltageAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"HighVoltageAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                ------------------------------欠电压告警-------------------------------
                                local LowVoltageAlarm = utils.And(status3,0x80)
                                if(LowVoltageAlarm == 0x00)
                                then
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"LowVoltageAlarm","","",0,string.format( "%d",0))
                                else
                                    Variable[VariableIndex+1] = utils.AppendVariable(VariableIndex,"LowVoltageAlarm","","",1,string.format( "%d",1))
                                end
                                VariableIndex = VariableIndex + 1
                                -------------------------------------------------------------
                                VariableMap = {
                                    Status = "0",
                                    Variable = Variable
                                }
                            end
                            rxBuf = {}
                            return VariableMap
                        end
                elseif(mFunCode == 0x05)
                then
                    mRegAddr = rxBuf[index+2]*256+rxBuf[index+3]
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+5,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+6+1]*256+rxBuf[index+6]
                    if(crc == crcTemp)
                    then
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap
                    end
                elseif(mFunCode == 0x06)
                then
                    mRegAddr = rxBuf[index+2]*256+rxBuf[index+3]
                    --取出数据域数据
                    local pdu = {}
                    for i=index,index+5,1 do
                        if rxBuf[i] ~= nil then
                            table.insert(pdu, rxBuf[i])
                        end
                    end
                    local VariableMapCRC = {
                        Variable = pdu
                    }
                    crc = GetCRCModbus(VariableMapCRC)
                    crcTemp = rxBuf[index+6+1]*256+rxBuf[index+6]
                    if(crc == crcTemp)
                    then
                        VariableMap = {
                            Status = "0",
                            Variable = Variable
                        }
                        rxBuf = {}
                        return VariableMap
                    end    
                else
                    index = index + 1
                end
            else
                index = index + 1
            end
        else
            rxBuf = {}
            return VariableMap
        end
    end

    rxBuf = {}
    return VariableMap
end
